#!/usr/bin/env ruby

require 'socket'

class Client
attr_accessor :socket,:cache,:send_wait,:type,:object,:reg_procs,:host,:port
 def initialize(host=nil,port=nil)
   @send_wait = []
   @returned={}
   @reg_procs={}
   @regexps=[]
   if host != nil and port != nil
     @host,@port = host,port
     connectToMuServer
   end
   @cache = {}
 end

 def loadServerModules(*modules)
   modules::each {|m|
     require "libs/utilities/main/modules/#{m}"
   }
 end

 def assignDefaultServerModule(type)
   @type = type
 end

 def connectToMuServer
   @socket = TCPSocket::new @host,@port
 end

 def read()
p "==\n==\n==\n===\n-===\n==n\\n"
   Thread::new do
    while line = @socket.gets
# p l
# p "00000000000000"+l+"---------";exit
       decide_string(line)
# p l
     end
   end
 end
 def return_data(string,ident)
   @returned[ident]=string
 end
 def decide_string(l)
# p l
   if @send_wait.find {|n| n!=nil}
# exit
      if l=~/# \-\-\- this is a header ([0-9]+)/
        @csw = @send_wait[$1.to_i]
      elsif l=~ /\# \-\-\- this is a footer ([0-9]+)/
# p l
iden = $1
        return_data(@csw.to_s,iden)
# p l

        @csw = nil
        @send_wait[iden.to_i]=nil
#         p @returned
      elsif @csw!=nil
        @csw << l
      else
        give(l) unless l == "\r\n"
      end
    else
# p l
      give(l)
    end
  end

  def give(line)
l = ""
buff = []
                   k=Sim::new
                   str = k.parse(line)
                   str.each {|s|
                     bclr=s[:bgcol]
                     fclr=s[:fgcol]
                     l << s[:text]
                     buff << [s[:text],bclr,fclr]
                     
                   }
    if registered(buff)
      
    else
      buff.each {|woo|
        $client.give *woo
      }
    end
  end
 def registered(buff)
   @reged = false
   string = ""
   buff.each {|woo|
     string << woo[0]
   } 
   @regexps.each {|reg|
     if string =~ reg
       @reged = true
       findRegProc(reg).call(reg,$~,buff)
       break
     end
   }
   return @reged
 end
 def register_regexp(array,prok)
   array.each {|reg|
     @reg_procs[reg]=prok
     @regexps << reg
   }
 end
 def findRegProc(reg)
   return @reg_procs[reg]
 end
 def recieve_data(command,mode)
# p command
   if mode == 1
     @send_wait << ""
     ident = @send_wait.length-1
     @socket.puts "th "+get_header
     @socket.puts command
     @socket.puts "th "+get_footer 
   else
     ident = @send_wait.length-1
     @socket.puts command
   end
# exit
   until @returned.has_key?(ident.to_s)
# p @returned;p ident
   end
# exit
   string = @returned[ident.to_s]
   @returned[ident.to_s]=""
   return string
 end

    def get_header(client=self)
# p "# --- this is a header #{@send_wait.length-1}"
      return "# --- this is a header #{@send_wait.length-1}" 
    end
    def get_footer(client=self)
      return "# --- this is a footer #{@send_wait.length-1}" 

    end
end
Thread::abort_on_exception = true
# until 1==0
# end

Thread::new do
cmd = ""
while cmd!="QUIT"
  cmd = gets.chomp
  eval cmd
end
end